#!/usr/bin/python
#-*-coding:utf-8-*-

import base64
import MySQLdb
import time 
import datetime
import calendar

#insert db 
def insert(member_id,head_url):
	isql = "INSERT INTO mem_icon(member_id, head_url) VALUES ('%d', '%s')" % \
       (member_id,head_url)
	try:
   	# 执行sql语句
   		cursor.execute(isql)
   	# 提交到数据库执行
   		db.commit()
	except:
   	# Rollback in case there is any error
   		db.rollback()

def getPic(mcmcard):
    try:
        encoded_string='data:image/jpeg;base64,'
        print mcmcard+".jpg"  
        with open(mcmcard+".jpg", "rb") as image_file:

              encoded_string = encoded_string + base64.b64encode(image_file.read())
              print '-----'+mcmcard
              return encoded_string
    except:
        print 'error '
        return ""


# encoded_string='data:image/jpeg;base64,'
# with open("000015"+".bmp", "rb") as image_file:
#     encoded_string = encoded_string + base64.b64encode(image_file.read())
#     print encoded_string;

# 打开数据库连接
db = MySQLdb.connect("172.16.5.30","bill","AdminSuper","tytg_data" )
cursor = db.cursor() 

print '===11111'
sql="select mem.id ,card.mc_code from rl_cardtype_card rl,mem_card_type ctype ,mem_member  mem, mem_card card where rl.card_type_Id=ctype.id  and card.member_id=mem.id and card.mc_code=rl.mc_code and ctype.value_type='DATELINE'   and mem.id not IN( select member_id from mem_icon ) "
   # 执行SQL语句
cursor.execute(sql)
   # 获取所有记录列表
results = cursor.fetchall()
for row in results:
	memid = row[0]
	mcmcard = row[1]

	print mcmcard
	stemp=getPic(mcmcard)
	if stemp<>"" :
		insert(memid,stemp)
	else:
		print "no pic "+mcmcard

# str=getPic("24997");
# print "===1111:"+str
# insert(24997,str)



# 关闭数据库连接
db.close()


